<?php 
/**
* student入口
*/
class Student_home extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		//登陆状态检测
		if(!$this->session->userdata('user_in'))
			redirect('home');
		//权限检测
		if($this->session->userdata('role_id') != 3)
			redirect('home');
	}

	function index()
	{
		if(date_default_timezone_get() != "Asia/Shanghai") 
			date_default_timezone_set("Asia/Shanghai");
		//考试日期晚于这个日期的试卷可以查看
		$date_early_limit_check=date("Y-m-d",strtotime("-7 day"));
		
		//准备数据		
		$username=$this->session->userdata('username');
		$this->load->model('user_model');
		$this->load->model('student_model');
		$this->load->model('major_model');
		$this->load->model('class_model');
		$this->load->model('report_model');
		$this->load->model('module_model');
		$this->load->model('lecture_model');
		$this->load->model('logic_class_model');

		//获取用户信息
		$user=$this->user_model->get_by_username($username);
		$student=$this->student_model->get_by_user_id($user['id']);

		//将学生的姓名放到session中
		$this->session->set_userdata(array('name'=>$student['name']));
		//获取学生参加的逻辑班
		$student_logic_class=$this->student_model->get_student_logic_class($student['id']);
		
		// echo "student_logic_class";
		// var_dump($student_logic_class);
		//获取所有逻辑班的信息
		$all_logic_class = array();
		foreach ($student_logic_class as $item) {
			$logic_class=$this->logic_class_model->get_by_id($item['logic_class_id']);
			array_push($all_logic_class, $logic_class);
		}

		// echo "all_logic_class";
		// var_dump($all_logic_class);

		//获取逻辑班下的考试模块
		$all_logic_class_module = array();
		foreach ($all_logic_class as $item) {
			$module_in_logic_class=$this->module_model->get_module_in_logic_class($item['lecture_id']);
			// echo "module_in_logic_class";
			// var_dump($module_in_logic_class);
			$lecture_info = $this->lecture_model->get_lecture_by_id($item['lecture_id']);
			//所有系统的模块和逻辑班信息叠加
			$module_add_logic_class = array();
			foreach ($module_in_logic_class as $item1) {

				//寻找每一个模块是否有可替换的定制模块
				$module_replace=$this->module_model->get_module_type_1_parent_teacher($item1['id'],$item['teacher_id']);
				// echo "module_replace";
				// var_dump($module_replace);

				//没有可替换的定制模块
				if (!$module_replace) {				
					//在report表中查找是否已经参加了考试					
					$report=$this->report_model->get_by_module_student_logic_class($item1['id'],$student['id'],$item['id']);
					// var_dump($item1['id']);
					// var_dump($student['id']);
					// var_dump($item['id']);
					//var_dump($report);
					//没有考试
					if (!$report) {
						$each_module_add_logic_class = array(
							'id' => $item1['id'], 
							'name' => $item1['name'], 
							'time_limit' => $item1['time_limit'], 
							'type' => $item1['type'], 
							'parent_id' => $item1['parent_id'], 
							'teacher_id' => $item1['teacher_id'], 
							'big_lecture_id' => $item1['big_lecture_id'],
							'logic_class_id' => $item['id'],
							'logic_class_number' => $item['number'],
							'logic_class_teacher_id' => $item['teacher_id'],
							'logic_class_type' => $item['type'],
							'lecture_id' => $item['lecture_id'],
							'lecture_name' => $lecture_info['name'],
							'big_lecture_name' => $lecture_info['big_lecture_name'],
							'report_id' => '0',
							'report_datetime' => '',
							'report_grades' => '',
							'report_state'=>2);
					}
					//已考试
					else{
						//检查当前的考试状态，根据时间判断，是否已经结束，还是可以继续进行
						if($report['state']==0)
						{
							//检查考试时间
							$now=date("Y-m-d H:i:s");
							$this->load->helper('compare_time');
							$time_past=compare_time_of_minute($report['datetime'],$now);
							if($time_past<=$item1['time_limit'])
							{
								//如果还有时间继续考试
							}
							else
							{
								//如果已经超过时间，判卷，更新report信息
								$this->refresh_grades($report['id']);
								$report=$this->report_model->get_by_module_student_logic_class($item1['id'],$student['id'],$item['id']);
							}
						}
						
						
						$report_date = date('Y-m-d',strtotime($report['datetime']));						
						if($report_date>=$date_early_limit_check)
						{
							$check_limit = 1;
						}
						else
						{
							$check_limit = 0;
						}
						
						$each_module_add_logic_class = array(
							'id' => $item1['id'], 
							'name' => $item1['name'], 
							'time_limit' => $item1['time_limit'], 
							'type' => $item1['type'], 
							'parent_id' => $item1['parent_id'], 
							'teacher_id' => $item1['teacher_id'], 
							'big_lecture_id' => $item1['big_lecture_id'], 
							'logic_class_id' => $item['id'],
							'logic_class_number' => $item['number'],
							'logic_class_teacher_id' => $item['teacher_id'], 
							'logic_class_type' => $item['type'],
							'lecture_id' => $item['lecture_id'],
							'lecture_name' => $lecture_info['name'],
							'big_lecture_name' => $lecture_info['big_lecture_name'],
							'report_id' => $report['id'],
							'report_datetime' =>  $report['datetime'],
							'report_grades' =>  $report['grades'],
							'report_state'=>$report['state'],
							'check_limit'=>$check_limit);
					}
					array_push($module_add_logic_class, $each_module_add_logic_class);
				}

				//有替换的模块
				else{
					foreach ($module_replace as $item2) {
						$report_replace=$this->report_model->get_by_module_student_logic_class($item2['id'],$student['id'],$item['id']);
						//没有考试
						if (!$report_replace) {
							$each_module_add_logic_class_replace = array(
								'id' => $item2['id'], 
								'name' => $item2['name'], 
								'time_limit' => $item2['time_limit'], 
								'type' => $item2['type'], 
								'parent_id' => $item2['parent_id'], 
								'teacher_id' => $item2['teacher_id'], 
								'big_lecture_id' => $item2['big_lecture_id'],
								'logic_class_id' => $item['id'],
								'logic_class_number' => $item['number'],
								'logic_class_teacher_id' => $item['teacher_id'],
								'logic_class_type' => $item['type'],
								'lecture_id' => $item['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name'],
								'report_id' => '0',
								'report_datetime' => '',
								'report_grades' => '',
								'report_state'=>2
								);
								

						}
						//已考试
						else{
							if($report_replace['state']==0)
							{
								//检查考试时间
								$now=date("Y-m-d H:i:s");
								$this->load->helper('compare_time');
								$time_past=compare_time_of_minute($report_replace['datetime'],$now);
								if($time_past<=$item1['time_limit'])
								{
									//如果还有时间继续考试
								}
								else
								{
									//如果已经超过时间，判卷，更新report信息
									$this->refresh_grades($report_replace['id']);
									$report_replace=$this->report_model->get_by_module_student_logic_class($item2['id'],$student['id'],$item['id']);
								}
							}

							$report_date = date('Y-m-d',strtotime($report_replace['datetime']));
							if($report_date>=$date_early_limit_check)
							{
								$check_limit = 1;
							}
							else
							{
								$check_limit = 0;
							}

							$each_module_add_logic_class_replace = array(
								'id' => $item2['id'], 
								'name' => $item2['name'], 
								'time_limit' => $item2['time_limit'], 
								'type' => $item2['type'], 
								'parent_id' => $item2['parent_id'], 
								'teacher_id' => $item2['teacher_id'], 
								'big_lecture_id' => $item2['big_lecture_id'],
								'logic_class_id' => $item['id'],
								'logic_class_number' => $item['number'],
								'logic_class_teacher_id' => $item['teacher_id'],
								'logic_class_type' => $item['type'],
								'lecture_id' => $item['lecture_id'],
								'lecture_name' => $lecture_info['name'],
								'big_lecture_name' => $lecture_info['big_lecture_name'],
								'report_id' => $report_replace['id'],
								'report_datetime' =>  $report_replace['datetime'],
								'report_grades' =>  $report_replace['grades'],
								'report_state'=>$report_replace['state'],
								'check_limit'=>$check_limit
							);
						}
						
						array_push($module_add_logic_class, $each_module_add_logic_class_replace);
					}				
					
				}
				
			}
			// echo "module_add_logic_class";
			// var_dump($module_add_logic_class);		
			
			array_push($all_logic_class_module, $module_add_logic_class);
		}

		//var_dump($all_logic_class_module);
		if(md5($username)==$user['password'])
		{
			$change_password=0;
		}
		else
		{
			$change_password=1;
		}

		$major=$this->major_model->get_by_id($student['major_id']);
		$class=$this->class_model->get_by_id($student['class_id']);
		
		$data = array();
		$data['title']='学生';
		$data['change_password']=$change_password;
		$data['student']=$student;
		$data['major']=$major;
		$data['class']=$class;
		$data['all_logic_class_module']=$all_logic_class_module;
		
		$this->load->view('student_home/index',$data);

	}

	//更新选课
	function update_register_logic_class()
	{
		$username=$this->session->userdata('username');
		$this->load->model('user_model');
		$this->load->model('student_model');
		$this->load->model('major_model');
		$this->load->model('class_model');
		$this->load->model('report_model');
		$this->load->model('module_model');
		$this->load->model('lecture_model');
		$this->load->model('logic_class_model');

		//获取用户信息
		$user=$this->user_model->get_by_username($username);
		$student=$this->student_model->get_by_user_id($user['id']);

		//将学生的姓名放到session中
		$this->session->set_userdata(array('name'=>$student['name']));
		//删除学生的选课
		$this->student_model->delete_relative_logic_classes($student['id']);
		//重新选课
		//获取所有要加入的逻辑班
         $logic_classes = $this->logic_class_model->get_by_type(2);
         $bool1 = $this->student_model->add_student_in_logic_classes($student['id'],$logic_classes);
		if($bool1)
			$this->session->set_flashdata('message','操作成功');
		else
			$this->session->set_flashdata('message','操作失败');

		redirect('student_home/index');
	}

	/**
	 * 更新一次考试的成绩，判分，内部函数，已经保证数据合法
	 * @param  [type] $report_id      [description]
	 * @return [type]                 [description]
	 */
	function refresh_grades($report_id)
	{
		if(date_default_timezone_get() != "Asia/Shanghai") 
			date_default_timezone_set("Asia/Shanghai");
		//根据数据库存储的选择判分
		$this->load->model('report_model');
		$this->load->model('question_model');
		$questions_in_report=$this->report_model->get_from_question_in_report($report_id);
		$grades=0;
		foreach ($questions_in_report as $item) {
			$real_question = $this->question_model->get_by_id($item['question_id']);//考题
			if ($item['choose'] == $real_question['answer']) {
				$grades++;
			}	
		}

		//更新report状态
		//将结果存入report表
		$report_item = array(
               'grades' => (int)($grades*100.0/count($questions_in_report)),
               'state' => 1
            );
		$this->report_model->update($report_id,$report_item);
		
	}


	/**
	 * 退出，删除session中的数据
	 * @return [type]
	 */
	function logout()
	{
		$this->session->sess_destroy();
		redirect('home');
	}   

	/**
	 * 进入考试页面
	 * @return [type]
	 */
	function test()
	{
		$data = array();
		$data['title']='电路基本问题及分析方法';
		$this->load->view('student_home/test',$data);
	}   

	/**
	 * 查看考试结果
	 * @return [type] [description]
	 */
	function check_detail()
	{
		$data['title']='查看考试结果';
		$this->load->view('student_home/check_detail',$data);
	}
	

	/**
	 * 修改密码
	 * @return [type] [description]
	 */
	function change_password()
	{
		//表单验证
		if($this->form_validation->run('student_home/change_password'))
		{
			//post方法
			//读取数据
			$old_password=$this->input->post('old_password');
			$new_password=$this->input->post('new_password');
			
			$username=$this->session->userdata('username');
			$this->load->model('user_model');
			$user=$this->user_model->check_user_valid($username,$old_password);
			if($user)
			{
				$user['password']=md5($new_password);
				if($this->user_model->update($user['id'],$user))
				{
					$this->session->set_flashdata('message','修改成功');
					redirect('student_home/index');
				}
				else
				{
					$this->session->set_flashdata('message','操作失败');
					redirect('student_home/index');
				}
					
			}
			else{
				$this->session->set_flashdata('message','密码错误，修改失败');
				redirect('student_home/index');	
			}
						
		}

		//get方法
		$data['title']='修改密码';
		$this->load->view('student_home/change_password',$data);
	}
}
 